package com.pool.activity;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemLongClickListener;

import com.pool.R;
import com.pool.activity.common.FullscreenListActivity;
import com.pool.common.Lists;
import com.pool.common.Maps;
import com.pool.common.Record;
import com.pool.common.Shoot;
import com.pool.store.RecordStore;
import com.pool.store.ShootStore;
import com.pool.store.Store;

/**
 * @author claveld@gmail.com (David Clavel)
 */
public class ViewLocalStore extends FullscreenListActivity implements OnItemLongClickListener {

  private final Store<Record> recordStore;
  private final Store<Shoot> shootStore;

  public ViewLocalStore() {
    this(new RecordStore(), new ShootStore());
  }
  
  public ViewLocalStore(Store<Record> recordStore, Store<Shoot> shootStore) {
    this.recordStore = recordStore;
    this.shootStore = shootStore;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.local_store);
    setTitle(R.string.local_store);
    
    getListView().setOnItemLongClickListener(this);
    
    refreshList();
  }
  
  private void refreshList() {
    List<Map<String, Object>> items = Lists.newArrayList();
    for (Record record : recordStore.getAll()) {
      addItem(items, record);
    }
    
    setListAdapter(new SimpleAdapter(
        this,
        items,
        R.layout.two_line_row,
        new String[] { "line1", "line2" },
        new int[] { R.id.text1, R.id.text2 }));
  }
  
  private void addItem(List<Map<String, Object>> list, Record record) {
    Map<String, Object> map = Maps.newHashMap();
    // TODO(claveld): extract this to a common place (used in History).
    Shoot shoot = shootStore.get(record.shootId);
    String suffix = "";
    if (shoot.isVictory()) {
      if (record.remainingBalls < 0) {
        suffix = "";
      } else if (record.remainingBalls == 0) {
        suffix = " | no balls remaining";
      } else if (record.remainingBalls == 1) {
        suffix = " | 1 ball remaining";
      } else {
        suffix = " | " + record.remainingBalls + " balls remaining";  
      }
    }

    map.put("line1", shoot.description);
    map.put("line2", record.player + ": " + record.timestamp + suffix);
    map.put("record", record);
    list.add(map);
  }
  
  @SuppressWarnings("unchecked")
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
    final Record record = (Record) map.get("record");
    
    List<Shoot> shoots = shootStore.getAll();
    int shootsCount = shoots.size();
    final String[] shootsDesc = new String[shootsCount];
    final Shoot[] shootsArray = new Shoot[shootsCount];
    int selected = 0;
    for (int i = 0; i < shootsCount; i++) {
      Shoot shoot = shoots.get(i);
      shootsDesc[i] = shoot.description;
      shootsArray[i] = shoot;
      if (shoot.id.equals(record.shootId)) {
        selected = i;
      }
    }

    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.edit_shoot);
    builder.setSingleChoiceItems(shootsDesc, selected, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        Shoot shoot = shootsArray[whichButton];
        record.shootId = shoot.id;
        recordStore.update(record);
        dialog.dismiss();
        refreshList();
      }
    });
    builder.create().show();
  }

  @SuppressWarnings("unchecked")
  public boolean onItemLongClick(AdapterView<?> l, View v, int position, long id) {
    Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
    final Record record = (Record) map.get("record");
    
    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.delete_shoot);
    builder.setPositiveButton(R.string.yes, new OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        recordStore.delete(record);
        refreshList();
      }
    });
    builder.setNegativeButton(R.string.no, null);
    builder.create().show();
    return true;
  }
}
